﻿using Alimentador.Foods;
using System.Collections.Generic;
using System.Windows.Controls;
using Alimentador.BottomGroup.Results;
using System.Windows;

namespace Alimentador.BottomGroup.Graphics
{
    public class PercentageValue
    {
        private double m_quantity = 0.00;
        private string m_name = "";

        /// <summary>
        /// Constructor with default values
        /// </summary>
        /// <param name="name">string</param>
        /// <param name="quantity">double</param>
        public PercentageValue(string name = "", double quantity = 0.00)
        {
            m_name = name;
            m_quantity = quantity;
        }

        public string GetPropertyName
        {
            get
            {
                return "";
                //return m_name;
            }
            set
            {
                m_name = value;
            }
        }

        public double Quantity
        {
            get
            {
                return System.Math.Round(m_quantity, 2);
            }
            set
            {
                m_quantity = value;
            }
        }
    }

    /// <summary>
    /// Interaction logic for GraphicsCtrl.xaml
    /// </summary>
    public partial class GraphicsCtrl : UserControl
    {
        public Dictionary<PropertyName, string> m_obtidos = new Dictionary<PropertyName, string>()
        {
            {PropertyName.Energia,   "Energia"},
            {PropertyName.Proteina,    "Proteina"},
            {PropertyName.GorduraTotal, "GorduraTotal"},
            {PropertyName.TotalHidratosCarbono,    "TotalHidratosCarbono"},
            {PropertyName.FibraAlimentar, "FibraAlimentar"},
            {PropertyName.Colesterol,  "Colesterol"}
        };

        private List<FoodProperty> m_valueList = new List<FoodProperty>();
        private List<FoodBase> m_foodList = new List<FoodBase>();
        private List<FoodProperty> m_recommendedValueList = new List<FoodProperty>();

        private int m_numDays = 1;

        private static bool m_IsShownInPopUp;

        /// <summary>
        /// Constructor
        /// </summary>
        public GraphicsCtrl()
        {
            InitializeComponent();

            PopulateChart();
            RefreshChart();
            RefreshRecommendedValues();
        }

        /// <summary>
        /// Fill the chart with default values
        /// </summary>
        private void PopulateChart()
        {
            AxisValues.Maximum = 200;
            AxisValues.Minimum = 0;

            //m_valueList.Add(new FoodProperty(PropertyName.Energia, 0.00));
            //m_valueList.Add(new FoodProperty(PropertyName.Proteina, 0.00));
            //m_valueList.Add(new FoodProperty(PropertyName.GorduraTotal, 0.00));
            //m_valueList.Add(new FoodProperty(PropertyName.TotalHidratosCarbono, 0.00));
            //m_valueList.Add(new FoodProperty(PropertyName.FibraAlimentar, 0.00));
            //m_valueList.Add(new FoodProperty(PropertyName.Colesterol, 0.00));

            //columnSeries.DataContext = m_valueList;

            string str = "Energia"; // m_minerals[PropertyName.Calcio];
            energieSeries.DataContext = new PercentageValue(str, 70);
            energieSeries.Title = str;


            //m_recommendedValueList.Add(new FoodProperty(PropertyName.Recomended, 0.00));
            //m_recommendedValueList.Add(new FoodProperty(PropertyName.Proteina, 0.00));
            //m_recommendedValueList.Add(new FoodProperty(PropertyName.GorduraTotal, 0.00));
            //m_recommendedValueList.Add(new FoodProperty(PropertyName.TotalHidratosCarbono, 0.00));
            //m_recommendedValueList.Add(new FoodProperty(PropertyName.FibraAlimentar, 0.00));
            //m_recommendedValueList.Add(new FoodProperty(PropertyName.Colesterol, 0.00));

            //recommendedSeries.DataContext = m_recommendedValueList;

            mainChart.FontSize = 10;
        }

        /// <summary>
        /// Refresh recommended values
        /// </summary>
        private void RefreshRecommendedValues()
        {
            foreach (FoodProperty fp in m_recommendedValueList)
            {
                fp.Quantity = 100;
            }
         
            //recommendedSeries.Refresh();
        }

        /// <summary>
        /// Refresh chart values
        /// </summary>
        private void RefreshChart()
        {
            double max = 100;

            double val = FoodFormulas.EnergyGraphic(m_foodList, m_numDays);
            energieSeries.DataContext = new PercentageValue[] { new PercentageValue(m_obtidos[PropertyName.Energia], val) };
            energieSeries.Title = m_obtidos[PropertyName.Energia] + ": " + val.ToString("F2") + "%";
            energieSeries.Refresh();
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.ProteinGraphic(m_foodList, m_numDays);
            proteinaSeries.DataContext = new PercentageValue[] { new PercentageValue(m_obtidos[PropertyName.Proteina], val) };
            proteinaSeries.Title = m_obtidos[PropertyName.Proteina] + ": " + val.ToString("F2") + "%";
            proteinaSeries.Refresh();
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.LipidsGraphic(m_foodList, m_numDays);
            lipidsSeries.DataContext = new PercentageValue[] { new PercentageValue(m_obtidos[PropertyName.GorduraTotal], val) };
            lipidsSeries.Title = m_obtidos[PropertyName.GorduraTotal] + ": " + val.ToString("F2") + "%";
            lipidsSeries.Refresh();
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.CarbonHidratesGraphic(m_foodList, m_numDays);
            hidratesSeries.DataContext = new PercentageValue[] { new PercentageValue(m_obtidos[PropertyName.TotalHidratosCarbono], val) };
            hidratesSeries.Title = m_obtidos[PropertyName.TotalHidratosCarbono] + ": " + val.ToString("F2") + "%";
            hidratesSeries.Refresh();
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.FiberGraphic(m_foodList, m_numDays);
            fibrasSeries.DataContext = new PercentageValue[] { new PercentageValue(m_obtidos[PropertyName.FibraAlimentar], val) };
            fibrasSeries.Title = m_obtidos[PropertyName.FibraAlimentar] + ": " + val.ToString("F2") + "%";
            fibrasSeries.Refresh();
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.ColesterolGraphic(m_foodList, m_numDays);
            colesterolSeries.DataContext = new PercentageValue[] { new PercentageValue(m_obtidos[PropertyName.Colesterol], val) };
            colesterolSeries.Title = m_obtidos[PropertyName.Colesterol] + ": " + val.ToString("F2") + "%";
            colesterolSeries.Refresh();
            max = (val > max) ? val + 10 : max;

            AxisValues.Maximum = max;
            mainChart.Refresh();
        }

        /// <summary>
        /// Hanlde the event for adding a Food to list
        /// </summary>
        /// <param name="foodList">List(Food)</param>
        /// <param name="numDays">int</param>
        internal void AddFood(List<FoodBase> foodList, int numDays = 1)
        {
            m_foodList = foodList;
            m_numDays = numDays;
            RefreshChart();
        }

        private void pieChart_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!m_IsShownInPopUp)
            {
                TheMainGrid.Children.Remove(mainChart);

                ChartPopUp cpp = new ChartPopUp(mainChart);
                cpp.Owner = Application.Current.MainWindow;
                m_IsShownInPopUp = true;
                cpp.ShowDialog();

                mainChart = cpp.getChartBack();

                TheMainGrid.Children.Add(mainChart);
                m_IsShownInPopUp = false;
            }
        }
    }
}
